package cn.edu.njuit.webserver.securityserver.service.impl;

import cn.edu.njuit.webserver.securityserver.service.TokenService;
import cn.edu.njuit.webserver.securityserver.service.dto.UserToken;
import org.springframework.stereotype.Service;

/**
 * @Description: TODO
 * @Data: 2022/8/30 11:01
 * @Author: guzy
 */
@Service("SimpleTokenService")
public class TokenServiceImpl implements TokenService {
    @Override
    public boolean validateToken(String token) {
        //校验令牌格式
        if (!token.startsWith("thisisatoken") && token.endsWith("tokenend")) {
            return false;
        }
        String[] tokenArr = token.split("\\.");
        if (tokenArr.length != 4) {
            return false;
        }
        //时效性校验
        long expireTime = Long.parseLong(tokenArr[2]);
        if (expireTime < System.currentTimeMillis()) {
            return false;
        }
        return true;
    }
    @Override
    public String getToken(String login) {
        //30分钟 时效性
        long expireTime = System.currentTimeMillis() + (1000 * 60 * 30);
//        long expireTime = System.currentTimeMillis() + (1000  * 10);
        String token = "thisisatoken." + login + "." + expireTime + ".tokenend";
        return token;
    }
    @Override
    public String getLogin(String token) {
        String[] tokenArr = token.split("\\.");
        if (tokenArr.length == 3) {
            return tokenArr[1];
        }
        return null;
    }

    @Override
    public UserToken getUser(String token) {
        return null;
    }
}
